(function () {
    var game2 = window.game2 = {
        width: 0,
        height: 0,

        asset: null,
        stage: null,
        ticker: null,
        clickScreen: null,
        bg: null,

        init: function () {
            this.asset = new game2.Asset();
            this.asset.on('complete', function (e) {
                this.asset.off('complete');
                this.initStage();
            }.bind(this));
            this.asset.load();
        },

        initStage: function () {


            this.width = maxWidth;
            this.height = maxHeigth;
            this.scale = scale;

            this.stage = new Hilo.Stage({
                renderType: 'canvas',
                width: this.width,
                height: this.height,
                scaleX: this.scale,
                scaleY: this.scale
            });
            document.getElementById("game2").appendChild(this.stage.canvas);

            this.ticker = new Hilo.Ticker(60);
            this.ticker.addTick(Hilo.Tween);
            this.ticker.addTick(this.stage);
            this.ticker.start();

            this.stage.enableDOMEvent(Hilo.event.POINTER_START, true);

            this.stage.onUpdate = this.onUpdate.bind(this);

            this.initScenes();
        },


        initScenes: function () {
            //鍑嗗鍦烘櫙
            this.clickScreen = new game2.Q2PlayScreen({
                width: this.width,
                height: this.height,
                image: this.asset
            }).addTo(this.stage);
        },
        onUpdate: function (delta) {
        }
    };
})
();
(function (ns) {
    var Asset = ns.Asset = Hilo.Class.create({
        Mixes: Hilo.EventMixin,

        queue: null,

        bg: null,
        play: null,
        shalou: null,
        load: function () {
            var resources = [
                {id: 'bg', src: 'images/q2-bg.jpg'},
                {id: 'play', src: 'images/play.png'},
                {id: 'shalou', src: 'images/shalou.png'}
            ];

            this.queue = new Hilo.LoadQueue();
            this.queue.add(resources);
            this.queue.on('complete', this.onComplete.bind(this));
            this.queue.start();
        },

        onComplete: function (e) {
            this.bg = this.queue.get('bg').content;
            this.play = this.queue.get('play').content;
            this.shalou = this.queue.get('shalou').content;
            this.queue.off('complete');
            this.fire('complete');
        }
    });
})(window.game2);

(function (ns) {

    var ClickScreen = ns.Q2PlayScreen = Hilo.Class.create({
        Extends: Hilo.Container,
        constructor: function (properties) {
            ClickScreen.superclass.constructor.call(this, properties);
            this.init(properties);
        },
        statue: 0,//0:表示初始状态，1：表示成功，2：表示失败
        startTime: null,
        range: [2400, 5000],
        reactionTime: null,
        init: function (properties) {
            var bg = new Hilo.Bitmap({
                if: "bg",
                image: properties.image.bg,
                rect: [0, 0, maxWidth, maxHeigth],
                x: 0,
                y: 0
            });
            this.addChild(bg);


            var playBtn = new Hilo.Button({
                id: 'play',
                image: properties.image.play,
                width: 247,
                height: 247,
                x: 142,
                y: 103
            })
            this.addChild(playBtn);

            this.playBtnText = new Hilo.Text({
                color: "yellow",
                font: "30px arial",
                text: "播放",
                textVAlign: "middle",
                x: 225,
                y: 367
            });
            this.addChild(this.playBtnText);


            var shalouBtn = new Hilo.Button({
                id: 'shalou',
                image: properties.image.shalou,
                width: 217,
                height: 217,
                x: 516,
                y: 121
            })
            this.addChild(shalouBtn);

            this.shalouBtnText = new Hilo.Text({
                color: "yellow",
                font: "30px arial",
                text: "",
                visible: false,
                textVAlign: "middle",
                width: 100,
                x: 540,
                y: 367
            });
            this.addChild(this.shalouBtnText);


            playBtn.on(Hilo.event.POINTER_START, function (e) {
                audio.play();
                this.reset();
                //设置起始时间
                this.startTime = new Date();
                this.playBtnText.text = ""
                this.shalouBtnText.text = "点击使用中亚"
                this.shalouBtnText.visible = true;

            }.bind(this))

            shalouBtn.on(Hilo.event.POINTER_START, function (e) {
                //设置反应时间
                this.reactionTime = new Date();
            }.bind(this))

        },
        onUpdate: function () {

            if (this.statue == -1 || this.statue == 1) {
                return;
            }

            if (this.statue == 0) {
                if (this.startTime) {
                    if (!this.reactionTime) {//处理没有点击的
                        if (new Date().getTime() > (this.startTime.getTime() + this.range[1])) {
                            this.shalouBtnText.visible = false;
                            this.playBtnText.text = "重播"
                            this.statue = -1;
                            window.q2Statue = -1;
                            console.log("挑战失败")
                        }
                    } else {
                        if (this.reactionTime.getTime() > (this.startTime.getTime() + this.range[1]) || this.reactionTime.getTime() < (this.startTime.getTime() + this.range[0])) {
                            this.statue = -1;
                            window.q2Statue = -1;
                            this.shalouBtnText.visible = false;
                            this.playBtnText.text = "重听"
                            console.log("挑战失败")
                        } else {
                            window.q2Statue = 1;
                            this.statue = 1;
                            console.log("挑战成功")
                        }
                    }
                }
            }
        },
        reset: function () {
            this.statue = 0;
            window.q2Statue = 0;
            this.startTime = null;
            this.reactionTime = null;
            this.playBtnText.text = "点击播放"
            this.shalouBtnText.visible = false
        }
    });

})(window.game2);